//
//  XQNavBarView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/1/27.
//

import UIKit
import SnapKit

class XQNavBarView: UIView {
    
    private lazy var bgView : UIView = {
        let v = UIView()
        v.backgroundColor = .clear
        return v
    }()
    
    private lazy var navView : UIView = {
        let v = UIView()
        v.backgroundColor = .clear;
        return v
    }()
    
    private lazy var backImageView: UIImageView = {
       let imageView = UIImageView()
        imageView.image = UIImage(named: "back_icon")
        return imageView
    }()
    
    private lazy var backBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.addTarget(self, action: #selector(backClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var titleLb : UILabel = {
        let lb = UILabel()
        lb.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        lb.textColor = .black
        return lb
    }()
    
    var title : String = "" {
        didSet {
            titleLb.text = title
        }
    }
    
    var backHandle: (() -> Void)?
    
    var hiddenBack : Bool = false {
        didSet {
            backImageView.isHidden = hiddenBack
            backBtn.isHidden = hiddenBack
        }
    }
    
    var titleColor : UIColor? {
        didSet {
            titleLb.textColor = titleColor
        }
    }
    
    var backIcon : String = "back_icon" {
        didSet {
            backImageView.image = UIImage(named: backIcon)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    

    private func makeUI() {
        addSubview(bgView)
        bgView.addSubview(navView)
        navView.addSubview(backImageView)
        navView.addSubview(titleLb)
        navView.addSubview(backBtn)
    }
    
    private func makeConstraints() {
        bgView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview().inset(0)
            make.height.equalTo(UIDevice.xq_topHeight())
        }
        navView.snp.makeConstraints { make in
            make.top.equalTo(UIDevice.xq_statusHeight())
            make.left.right.bottom.equalToSuperview().inset(0)
        }
        backImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(20)
            make.width.equalTo(22)
            make.height.equalTo(19)
        }
        titleLb.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        backBtn.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(65)
        }
    }
    
    @objc private func backClick() {
        backHandle?()
    }

}
